/** @noSelfInFile **/

import {ITimerAdapter, timeAdapter} from "../../adapter/adapterInterface/model2/ITimerAdapter";
import {GlobalModel} from "../model1/GlobalModel";
import {HandleModel} from "../model1/HandleModel";

export class TimerModel implements  HandleModel {
  public handle: any;
  id: string;
  nikeName: string;

  timerAdapter: ITimerAdapter;
  timerDialog: any;

  constructor(name: string) {
    this.nikeName = name;

    this.timerAdapter = timeAdapter.adapter
    const handle = this.timerAdapter.createHandle();
    this.bindHandle(handle);
  }

  getHandleId(): string {
    return this.id;
  }

  public pause() {
    this.timerAdapter.pause(this.handle);
  }
  public resume() {
    this.timerAdapter.resume(this.handle);
  }
  public start(timeout: number, periodic: boolean, handlerFunc: (this: void) => void) {
    this.timerAdapter.start(this.handle, timeout, periodic, handlerFunc);
  }

  public showDialog(text: string) {
    this.timerDialog = this.timerAdapter.showDialog(this.handle, text);
  }

  destroy() {
    this.timerAdapter.destroy(this.handle, this.timerDialog);
    this.handle = undefined;
    this.timerDialog = undefined;
  }

  bindHandle(handle: any) {
    this.timerAdapter.bindHandle(handle);
    this.handle = handle;
    this.id = GlobalModel.getHandleId(this.handle);
  }

  equal(handle: TimerModel): boolean {
    return this.timerAdapter.equal(this.handle, handle.handle)
  }

  getRemaining(): number {
    return this.timerAdapter.getRemaining(this.handle)
  }

  isValid(): boolean {
    return this.timerAdapter.isValid(this.handle)
  }

}
